package principal;


import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import modificar.ModifyInterface;
import consulta.QueryInterface;
import crear.CreateDocInterface;

public class MainInterface extends JFrame{

	private static final long serialVersionUID = 1L;
	// Botones
	JButton crear, modificar, consultar, salir;
	public static ManagerDB managerDB;
	
	public MainInterface(){
		super("Proyecto");
		this.setResizable(false);
		centerWindow(3);
		this.add(getMainPanel());
	}
	
	private void centerWindow(int num){
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.setBounds((screenSize.width - this.getWidth()) / num,
				(screenSize.height - this.getHeight()) / num, 
				this.getWidth(),
				this.getHeight());
    }
	
	public static void centerWindow(JDialog dialog, int num){
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		dialog.setBounds((screenSize.width - dialog.getWidth()) / num,
				(screenSize.height - dialog.getHeight()) / num, 
				dialog.getWidth(),
				dialog.getHeight());
    }
	
	public JPanel getMainPanel(){
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(4,1));
		
		// Botones
		crear = new JButton("Crear");
		crear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainInterface.this.setVisible(false);
				CreateDocInterface cDIn = new CreateDocInterface(MainInterface.this);
				cDIn.setVisible(true);
				cDIn.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				
			}
		});
		modificar = new JButton("Modificar");
		modificar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainInterface.this.setVisible(false);
				ModifyInterface mIn = new ModifyInterface(MainInterface.this);
				mIn.pack();
				mIn.setVisible(true);
				mIn.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			}
		});
		consultar = new JButton("Consultar");
		consultar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainInterface.this.setVisible(false);
				QueryInterface qIn = new QueryInterface(MainInterface.this);
				qIn.setSize(200, 100);
				qIn.setVisible(true);
				qIn.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			}
		});
		salir = new JButton("Salir");
		salir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO: quitar
				try{
				managerDB.rs = managerDB.st.executeQuery("Select * from documento");
				
				// Escribimos la consulta
				String result = "";
				while(managerDB.rs.next()){
					for (int k = 1; k <= managerDB.rs.getMetaData().getColumnCount(); k++){
						result = managerDB.rs.getString(k);
					}
					System.out.print(result+"\n");
				}
				}catch(Exception ex){}
				
				managerDB.close();
				MainInterface.this.dispose();
			}
		});
		
		mainPanel.add(crear);
		mainPanel.add(modificar);
		mainPanel.add(consultar);
		mainPanel.add(salir);
		
		return mainPanel;
	}

	
	
	/* --------------------------- MAIN -------------------------- */
	public static void main(String args[]){
		// args[0] : Fichero SQL en el que se encuentre la base de datos actual. 
		managerDB = new ManagerDB("./DocHistoria.sql");
		MainInterface g = new MainInterface();
		g.setSize(200, 150);
		g.setVisible(true);	
		g.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}
